<?php
/**
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `jid` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `lastNick` varchar(255) default NULL,
  `actions` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) 
 */
 
/**
 * @package	minBot
 * @author	Martin Sadový <sodae@email.cz>
 * @copyright   2009 Martin Sadový
 */
class Auth
{

    protected $table = 'users';

    /**
     * Get User data
     * @param string $jid
     * @return DibiRow
     */
    private function getJid($jid)
    {
        return dibi::select('*')->from($this->table)->where('jid=%s',$jid)->fetch();
    }

    /**
     * Get User data
     * @param string $nick
     * @return DibiRow
     */
    private function getLastNick($nick)
    {
        return dibi::select('*')->from($this->table)->where('lastNick=%s',$nick)->fetch();
    }

    /**
     * Get Jid from last Nick in Room
     * @param string $nick
     * @return string
     */
    public function getJidFromLastNick($nick)
    {
        if(empty ($nick))
            return null;

        $fetch = $this->getLastNick($nick);
        return $fetch['jid'];
    }

    /**
     * Get Actions of User
     * @param string $jid
     * @return array
     */
    public function getAllowActions($jid)
    {
        $fetch = $this->getJid($jid);
        return (array) explode(',',$fetch['actions']);
    }

    /**
     * Is allow action
     * @param string $jId
     * @param string $command
     * @return bool
     */
    public function isAllowAction($jId, $command)
    {
        return in_array(strtolower($command), $this->getAllowActions($jId));
    }

    /**
     * Is denied action
     * @param string $jId
     * @param string $command
     * @return bool
     */
    public function isDeniedAction($jId, $command)
    {
        return !$this->isAllowAction($jId, $command);
    }

    /**
     * Is user in room register
     * @param string $jId
     * @param string $command
     * @return bool
     */
    public function registerLastNick($jid, $nick)
    {        
        dibi::update($this->table, array('lastNick' => $nick) )->where('jid = %s', $jid)->execute(true);
    }

    /**
     * Register Jid
     * @param string $jid
     * @param string $password
     * @param string|array $actions
     */
    public function registerJid($jid, $password, $actions = '')
    {

        if(is_array($actions))
            $actions = implode(',', $actions);
            
        dibi::insert($this->table, array(
                'jid' => $jid,
                'password' => $password,
                'actions' => $actions
            ) )->execute(true);
    }

    /**
     * Is register Jid 
     * @param string $jid
     * @return bool
     */
    public function isRegisterJid($jid)
    {
        return (bool) $this->getJid($jid);
    }

    /**
     * Update user password
     * @param string $jid
     * @param string $password
     */
    public function changePassword($jid, $password)
    {
        dibi::update($this->table, array('password' => $password) )->where('jid = %s', $jid)->execute(true);
    }
}

?>